#include <iostream>

using namespace std;

int main()
{
	int t; cin >> t;
	while(t--)
	{
		int x, y, a, b; cin >> x >> y >> a >> b;
		int ans = b - y;
		if(ans < 0) cout << -1 << endl;
		else
		{
			x += ans;
			if(x < a) cout << -1 << endl;
			else cout << ans + abs(x - a) << endl;
		}
		
	}
	return 0;
 } 
